package cn.wjc.demo.partone.controller;

import cn.wjc.demo.common.entity.Student;
import cn.wjc.demo.common.entity.Teacher;
import cn.wjc.demo.partone.client.PartoneFeignService;
import cn.wjc.demo.partone.service.ITeacherService;
import cn.wjc.demo.partone.service.PrintService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("partone")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PartoneTestFeignController {


    private final PrintService printService;
    private final PartoneFeignService partoneFeignService;
    private final ITeacherService teacherService;

    @GetMapping("/print")
    public String printSomething(String s) {
        printService.printSomething(s);
        return "打印成功";
    }

    @GetMapping("/local/add/teacher")
    public String localAddTeacher(Teacher teacher) {
        teacherService.addTeacher(teacher);
        return "本地调用成功，教师【"+teacher.toString()+"】添加完成";
    }

    @GetMapping("/queryList/teacher")
    public void queryListTeacher(Teacher teacher) {
        List<Teacher> list = teacherService.queryListTeacher(teacher);
        System.out.println(list.toString());
    }

    @GetMapping("/test2/{s}")
    public void test2(@PathVariable String s) {
         partoneFeignService.remoteCallParttwoDoSomething(s);
    }

    @PostMapping("/add/teacher")
    public void addTeacher(@RequestBody Teacher teacher) {
        System.out.println("seata全局事务id====================>"+ RootContext.getXID());
        System.out.println("正在被partthree远程调用中...");
        System.out.println("执行【新增老师】的请求...");
        teacherService.addTeacher(teacher);
        System.out.println("partone 本地【新增老师】结束！");
    }

    @GlobalTransactional(rollbackFor = Exception.class)
    @GetMapping("/remote/add/student")
    public String remoteCallParttwoAddStudent() {
        System.out.println("seata全局事务id====================>"+ RootContext.getXID());
        Student student = new Student();
        Double sId = (Math.random()*10000+1000);
        student.setSId(sId.toString());
        student.setSName("网易云");
        partoneFeignService.remoteCallParttwoAddStudent(student);

        log.info("新增成功");
        return "远程调用成功，已添加学生";
    }
}
